package com.cmii.sjw.department.control.jsc.task;

import cn.hutool.http.*;
import com.cmii.sjw.department.control.common.JsonUtil;
import com.cmii.sjw.department.control.jsc.util.ApiCaller;
import com.cmii.sjw.department.control.request.IRSUtil;
import com.cmii.sjw.department.control.request.bean.DataRequest;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author jfh
 * @Date 2025年07月01日 11:08
 */
@Slf4j
@Component
public class XxlJobHandler {
    /**
     * 更新状态
     **/
    @XxlJob("UpdateStatus")
    public void updateStatus() {

        String jobParam = XxlJobHelper.getJobParam();
        log.info("任务执行" + jobParam);
    }


    /**
     * 第三方接口调用
     **/

    @XxlJob("yeyssb")
    public void yeyssb() {
        String url = XxlJobHelper.getJobParam();

        if (url == null || url.trim().isEmpty()) {
            XxlJobHelper.handleFail("任务参数为空，接口地址不能为空");
            return;
        }

        url = url.trim();
        try {
            log.info("请求接口地址：{}", url);

            Map<String, Object> paramMap = new HashMap<>();
            String response = HttpUtil.post(url, paramMap, 30000);
            log.info("接口响应：{}", response);

            // TODO: 解析、存库等处理

            XxlJobHelper.handleSuccess("接口调用成功");
        } catch (Exception e) {
            log.error("接口调用失败：{}", url, e);
            XxlJobHelper.handleFail("接口调用失败：" + e.getMessage());
        }
    }

    /**
     * 矛盾纠纷化解率接口调用
     **/

    @XxlJob("mdjfhjl")
    public void mdjfhjl() {
        String url = "https://drs.dsjfzj.cq.cegn:11007/restapi/prod/IC50000020240415000054/xdata/tbl/api/execute/50d2d672-b85f-4c71-9731-37e05398c3b8";

        if (url == null || url.trim().isEmpty()) {
            XxlJobHelper.handleFail("任务参数为空，接口地址不能为空");
            return;
        }

        try {
            String accessKey = "BCDSGA_af6c5c9363f6a93d1f5b2c4dfd6fb316";
            String secretKey = "BCDSGS_796070522f33a59b1da7e7f0669c6b02";

            // 获取 token
            String token = IRSUtil.getToken(accessKey, secretKey);

            Map<String, String> bodyMap = new HashMap<>();
            bodyMap.put("", "");

            // 转成 JSON 字符串
            String bodyJson = JsonUtil.toJson(new DataRequest(bodyMap));

            // 发起 POST 请求
            HttpRequest request = HttpRequest.of(url)
                    .method(Method.POST)
                    .header(IRSUtil.TOKEN_HEADER, token)
                    .body(bodyJson)
                    .contentType(ContentType.JSON.getValue());

            HttpResponse response = request.execute();

            if (response.getStatus() != HttpStatus.HTTP_OK) {
                log.error("接口调用失败，状态码：{}，响应：{}", response.getStatus(), response.body());
                XxlJobHelper.handleFail("请求失败，状态码：" + response.getStatus());
                return;
            }

            String result = response.body();
            log.info("接口调用成功，返回内容：{}", result);
            XxlJobHelper.handleSuccess("任务执行成功");

            // TODO: 解析、存库等处理

        } catch (Exception e) {
            log.error("接口调用失败", e);
            XxlJobHelper.handleFail("接口调用失败：" + e.getMessage());
        }
    }
    /**
     * 重大涉稳风险线索查证率
     **/

    @XxlJob("zdswfxxsczl")
    public void zdswfxxsczl() {
        String url = "https://drs.dsjfzj.cq.cegn:11007/restapi/prod/IC50000020250227000003/api/zfwpacqdata/irsGeneral/majorRiskVerRate";

        if (url == null || url.trim().isEmpty()) {
            XxlJobHelper.handleFail("任务参数为空，接口地址不能为空");
            return;
        }

        try {
            String accessKey = "BCDSGA_6d6f2c3ef8119c9e42b77ce3b3192904";
            String secretKey = "BCDSGS_55df1a4085024a11d30e2c2e898db0e9";

            // 获取 token
            String token = IRSUtil.getToken(accessKey, secretKey);

            Map<String, String> bodyMap = new HashMap<>();
            bodyMap.put("", "");

            // 转成 JSON 字符串
            String bodyJson = JsonUtil.toJson(new DataRequest(bodyMap));

            // 发起 POST 请求
            HttpRequest request = HttpRequest.of(url)
                    .method(Method.POST)
                    .header(IRSUtil.TOKEN_HEADER, token)
                    .body(bodyJson)
                    .contentType(ContentType.JSON.getValue());

            HttpResponse response = request.execute();

            if (response.getStatus() != HttpStatus.HTTP_OK) {
                log.error("接口调用失败，状态码：{}，响应：{}", response.getStatus(), response.body());
                XxlJobHelper.handleFail("请求失败，状态码：" + response.getStatus());
                return;
            }

            String result = response.body();
            log.info("接口调用成功，返回内容：{}", result);
            XxlJobHelper.handleSuccess("任务执行成功");

            // TODO: 解析、存库等处理

        } catch (Exception e) {
            log.error("接口调用失败", e);
            XxlJobHelper.handleFail("接口调用失败：" + e.getMessage());
        }
    }

    /**
     * 涉稳风险预警总数
     **/

    @XxlJob("swfxyjzs")
    public void swfxyjzs() {
        String url = "https://drs.dsjfzj.cq.cegn:11007/restapi/prod/IC50000020250227000005/api/zfwpacqdata/irsGeneral/riskEarlyWarningTotal";

        if (url == null || url.trim().isEmpty()) {
            XxlJobHelper.handleFail("任务参数为空，接口地址不能为空");
            return;
        }

        try {
            String accessKey = "BCDSGA_4aaf9a1641881f788e9655cbba3aa57f";
            String secretKey = "BCDSGS_50d569419dc55ab4605aa331fbaf77e9";

            // 获取 token
            String token = IRSUtil.getToken(accessKey, secretKey);

            Map<String, String> bodyMap = new HashMap<>();
            bodyMap.put("", "");

            // 转成 JSON 字符串
            String bodyJson = JsonUtil.toJson(new DataRequest(bodyMap));

            // 发起 POST 请求
            HttpRequest request = HttpRequest.of(url)
                    .method(Method.POST)
                    .header(IRSUtil.TOKEN_HEADER, token)
                    .body(bodyJson)
                    .contentType(ContentType.JSON.getValue());

            HttpResponse response = request.execute();

            if (response.getStatus() != HttpStatus.HTTP_OK) {
                log.error("接口调用失败，状态码：{}，响应：{}", response.getStatus(), response.body());
                XxlJobHelper.handleFail("请求失败，状态码：" + response.getStatus());
                return;
            }

            String result = response.body();
            log.info("接口调用成功，返回内容：{}", result);
            XxlJobHelper.handleSuccess("任务执行成功");

            // TODO: 解析、存库等处理

        } catch (Exception e) {
            log.error("接口调用失败", e);
            XxlJobHelper.handleFail("接口调用失败：" + e.getMessage());
        }
    }

    /**
     * 渝事好商量_议题管理_群众建议_接口
     **/

    @XxlJob("yshsl")
    public void yshsl() {
        String url = "https://drs.dsjfzj.cq.cegn:11007/restapi/prod/IC50000020250626000093/xdata/tbl/api/execute/f8a632ed-f1cf-4502-a277-2f77ab2554da";

        if (url == null || url.trim().isEmpty()) {
            XxlJobHelper.handleFail("任务参数为空，接口地址不能为空");
            return;
        }

        try {
            String accessKey = "BCDSGA_48dca2e4c1924a8e4d35ee12ba6578b3";
            String secretKey = "BCDSGS_35ab93aa2bcdf0628890d52c6e130423";

            // 获取 token
            String token = IRSUtil.getToken(accessKey, secretKey);

            Map<String, String> bodyMap = new HashMap<>();
            bodyMap.put("", "");

            // 转成 JSON 字符串
            String bodyJson = JsonUtil.toJson(new DataRequest(bodyMap));

            // 发起 POST 请求
            HttpRequest request = HttpRequest.of(url)
                    .method(Method.POST)
                    .header(IRSUtil.TOKEN_HEADER, token)
                    .body(bodyJson)
                    .contentType(ContentType.JSON.getValue());

            HttpResponse response = request.execute();

            if (response.getStatus() != HttpStatus.HTTP_OK) {
                log.error("接口调用失败，状态码：{}，响应：{}", response.getStatus(), response.body());
                XxlJobHelper.handleFail("请求失败，状态码：" + response.getStatus());
                return;
            }

            String result = response.body();
            log.info("接口调用成功，返回内容：{}", result);
            XxlJobHelper.handleSuccess("任务执行成功");

            // TODO: 解析、存库等处理

        } catch (Exception e) {
            log.error("接口调用失败", e);
            XxlJobHelper.handleFail("接口调用失败：" + e.getMessage());
        }
    }
    /**
     * 数据库_现行有效市人大规范性文件_接口：
     **/

    @XxlJob("sjkxx")
    public void sjkxx() {
        String url = "https://drs.dsjfzj.cq.cegn:11007/restapi/prod/IC50000020250627000089/xdata/tbl/api/execute/65382be8-8247-4a07-b39c-8906d33ad501";

        if (url == null || url.trim().isEmpty()) {
            XxlJobHelper.handleFail("任务参数为空，接口地址不能为空");
            return;
        }

        try {
            String accessKey = "BCDSGA_db662783616e6750f37badf0bb4057e9";
            String secretKey = "BCDSGS_3903edab880f1250e8ee81928e60c035";

            // 获取 token
            String token = IRSUtil.getToken(accessKey, secretKey);

            Map<String, String> bodyMap = new HashMap<>();
            bodyMap.put("", "");

            // 转成 JSON 字符串
            String bodyJson = JsonUtil.toJson(new DataRequest(bodyMap));

            // 发起 POST 请求
            HttpRequest request = HttpRequest.of(url)
                    .method(Method.POST)
                    .header(IRSUtil.TOKEN_HEADER, token)
                    .body(bodyJson)
                    .contentType(ContentType.JSON.getValue());

            HttpResponse response = request.execute();

            if (response.getStatus() != HttpStatus.HTTP_OK) {
                log.error("接口调用失败，状态码：{}，响应：{}", response.getStatus(), response.body());
                XxlJobHelper.handleFail("请求失败，状态码：" + response.getStatus());
                return;
            }

            String result = response.body();
            log.info("接口调用成功，返回内容：{}", result);
            XxlJobHelper.handleSuccess("任务执行成功");

            // TODO: 解析、存库等处理

        } catch (Exception e) {
            log.error("接口调用失败", e);
            XxlJobHelper.handleFail("接口调用失败：" + e.getMessage());
        }
    }

    /**
     * 批量保存评价数据（信创）：
     **/

    @XxlJob("plbcpjsj")
    public void plbcpjsj() {
        String url = "https://drs.dsjfzj.cq.cegn:11007/restapi/prod/IC50000020250526000040/hcpfw/main/evaluatesub/dataCollectionBatch";

        if (url == null || url.trim().isEmpty()) {
            XxlJobHelper.handleFail("任务参数为空，接口地址不能为空");
            return;
        }

        try {
            String accessKey = "BCDSGA_fe1d57e6b3e47cd54a0de7961b8b1839";
            String secretKey = "BCDSGS_c060f0c3d7262bfd2aac9b2f01198643";

            // 获取 token
            String token = IRSUtil.getToken(accessKey, secretKey);

            Map<String, String> bodyMap = new HashMap<>();
            bodyMap.put("", "");

            // 转成 JSON 字符串
            String bodyJson = JsonUtil.toJson(new DataRequest(bodyMap));

            // 发起 POST 请求
            HttpRequest request = HttpRequest.of(url)
                    .method(Method.POST)
                    .header(IRSUtil.TOKEN_HEADER, token)
                    .body(bodyJson)
                    .contentType(ContentType.JSON.getValue());

            HttpResponse response = request.execute();

            if (response.getStatus() != HttpStatus.HTTP_OK) {
                log.error("接口调用失败，状态码：{}，响应：{}", response.getStatus(), response.body());
                XxlJobHelper.handleFail("请求失败，状态码：" + response.getStatus());
                return;
            }

            String result = response.body();
            log.info("接口调用成功，返回内容：{}", result);
            XxlJobHelper.handleSuccess("任务执行成功");

            // TODO: 解析、存库等处理

        } catch (Exception e) {
            log.error("接口调用失败", e);
            XxlJobHelper.handleFail("接口调用失败：" + e.getMessage());
        }
    }

    /**
     * 涉稳类教育领域风险事件调度信息_接口：
     **/

    @XxlJob("swljylyfxsjddxx")
    public void swljylyfxsjddxx() {
        String url = "https://drs.dsjfzj.cq.cegn:11007/restapi/prod/IC50000020250704000031/xdata/tbl/api/execute/40b9cb59-31e7-470b-b246-a8070856ac47";

        if (url == null || url.trim().isEmpty()) {
            XxlJobHelper.handleFail("任务参数为空，接口地址不能为空");
            return;
        }

        try {
            String accessKey = "BCDSGA_5d41ba7c23c86cd74390c7c2c292f94f";
            String secretKey = "BCDSGS_b971b3738e37ec12c3b01f11479a8056";

            // 获取 token
            String token = IRSUtil.getToken(accessKey, secretKey);

            Map<String, String> bodyMap = new HashMap<>();
            bodyMap.put("", "");

            // 转成 JSON 字符串
            String bodyJson = JsonUtil.toJson(new DataRequest(bodyMap));

            // 发起 POST 请求
            HttpRequest request = HttpRequest.of(url)
                    .method(Method.POST)
                    .header(IRSUtil.TOKEN_HEADER, token)
                    .body(bodyJson)
                    .contentType(ContentType.JSON.getValue());

            HttpResponse response = request.execute();

            if (response.getStatus() != HttpStatus.HTTP_OK) {
                log.error("接口调用失败，状态码：{}，响应：{}", response.getStatus(), response.body());
                XxlJobHelper.handleFail("请求失败，状态码：" + response.getStatus());
                return;
            }

            String result = response.body();
            log.info("接口调用成功，返回内容：{}", result);
            XxlJobHelper.handleSuccess("任务执行成功");

            // TODO: 解析、存库等处理

        } catch (Exception e) {
            log.error("接口调用失败", e);
            XxlJobHelper.handleFail("接口调用失败：" + e.getMessage());
        }
    }

    /**
     * 评价数据对接（信创）：
     **/

    @XxlJob("pjsjdj")
    public void pjsjdj() {
        String url = "https://drs.dsjfzj.cq.cegn:11007/restapi/prod/IC50000020250526000019/hcpfw/main/evaluatesub/dataCollection";

        if (url == null || url.trim().isEmpty()) {
            XxlJobHelper.handleFail("任务参数为空，接口地址不能为空");
            return;
        }

        try {
            String accessKey = "BCDSGA_0056518d80115a58adc8376a6c316cf6";
            String secretKey = "BCDSGS_f960a9c01a82e00e978fbbe6e1b91406";

            // 获取 token
            String token = IRSUtil.getToken(accessKey, secretKey);

            Map<String, String> bodyMap = new HashMap<>();
            bodyMap.put("", "");

            // 转成 JSON 字符串
            String bodyJson = JsonUtil.toJson(new DataRequest(bodyMap));

            // 发起 POST 请求
            HttpRequest request = HttpRequest.of(url)
                    .method(Method.POST)
                    .header(IRSUtil.TOKEN_HEADER, token)
                    .body(bodyJson)
                    .contentType(ContentType.JSON.getValue());

            HttpResponse response = request.execute();

            if (response.getStatus() != HttpStatus.HTTP_OK) {
                log.error("接口调用失败，状态码：{}，响应：{}", response.getStatus(), response.body());
                XxlJobHelper.handleFail("请求失败，状态码：" + response.getStatus());
                return;
            }

            String result = response.body();
            log.info("接口调用成功，返回内容：{}", result);
            XxlJobHelper.handleSuccess("任务执行成功");

            // TODO: 解析、存库等处理

        } catch (Exception e) {
            log.error("接口调用失败", e);
            XxlJobHelper.handleFail("接口调用失败：" + e.getMessage());
        }
    }
    /**
     * 两江新区教育局信息统计台账_接口：
     **/

    @XxlJob("ljxqjyjxxtjtz")
    public void ljxqjyjxxtjtz() {
        String url = "https://drs.dsjfzj.cq.cegn:11007/restapi/prod/IC50000020250707000056/xdata/tbl/api/execute/2491d9b7-b54f-47e0-a848-2f5bd2ec767e";

        if (url == null || url.trim().isEmpty()) {
            XxlJobHelper.handleFail("任务参数为空，接口地址不能为空");
            return;
        }

        try {
            String accessKey = "BCDSGA_7796424deb8500e70746fca2a74d87bb";
            String secretKey = "BCDSGS_fa85b1bf1894a8ae890151de462e74d4";

            // 获取 token
            String token = IRSUtil.getToken(accessKey, secretKey);

            Map<String, String> bodyMap = new HashMap<>();
            bodyMap.put("", "");

            // 转成 JSON 字符串
            String bodyJson = JsonUtil.toJson(new DataRequest(bodyMap));

            // 发起 POST 请求
            HttpRequest request = HttpRequest.of(url)
                    .method(Method.POST)
                    .header(IRSUtil.TOKEN_HEADER, token)
                    .body(bodyJson)
                    .contentType(ContentType.JSON.getValue());

            HttpResponse response = request.execute();

            if (response.getStatus() != HttpStatus.HTTP_OK) {
                log.error("接口调用失败，状态码：{}，响应：{}", response.getStatus(), response.body());
                XxlJobHelper.handleFail("请求失败，状态码：" + response.getStatus());
                return;
            }

            String result = response.body();
            log.info("接口调用成功，返回内容：{}", result);
            XxlJobHelper.handleSuccess("任务执行成功");

            // TODO: 解析、存库等处理

        } catch (Exception e) {
            log.error("接口调用失败", e);
            XxlJobHelper.handleFail("接口调用失败：" + e.getMessage());
        }
    }
    /**
     * 渝中区医疗保障局家庭经济困难大学生名单_接口
     **/

    @XxlJob("yzqylbzjjtjjkndxsmd")
    public void yzqylbzjjtjjkndxsmd() {
        String url = "https://drs.dsjfzj.cq.cegn:11007/restapi/prod/IC50000020250613000226/xdata/tbl/api/execute/33a17967-1bee-4ad7-bf25-158c815f4933";

        if (url == null || url.trim().isEmpty()) {
            XxlJobHelper.handleFail("任务参数为空，接口地址不能为空");
            return;
        }

        try {
            String accessKey = "BCDSGA_9e70091ef7a6164728138cab428daf9b";
            String secretKey = "BCDSGS_b866163443a391fe406c8bcb8a5d6f20";

            // 获取 token
            String token = IRSUtil.getToken(accessKey, secretKey);

            Map<String, String> bodyMap = new HashMap<>();
            bodyMap.put("", "");

            // 转成 JSON 字符串
            String bodyJson = JsonUtil.toJson(new DataRequest(bodyMap));

            // 发起 POST 请求
            HttpRequest request = HttpRequest.of(url)
                    .method(Method.POST)
                    .header(IRSUtil.TOKEN_HEADER, token)
                    .body(bodyJson)
                    .contentType(ContentType.JSON.getValue());

            HttpResponse response = request.execute();

            if (response.getStatus() != HttpStatus.HTTP_OK) {
                log.error("接口调用失败，状态码：{}，响应：{}", response.getStatus(), response.body());
                XxlJobHelper.handleFail("请求失败，状态码：" + response.getStatus());
                return;
            }

            String result = response.body();
            log.info("接口调用成功，返回内容：{}", result);
            XxlJobHelper.handleSuccess("任务执行成功");

            // TODO: 解析、存库等处理

        } catch (Exception e) {
            log.error("接口调用失败", e);
            XxlJobHelper.handleFail("接口调用失败：" + e.getMessage());
        }
    }
}
